package view.transitions;

import java.awt.Point;
import java.awt.image.BufferedImage;

public abstract class Animator {
	public static int	MAX_FRAMES	= 600;

	protected BufferedImage	original;
	protected BufferedImage	overlay;
	protected Point			originalPosition;
	protected Point			overlayPosition;
	protected float			originalOpacity;
	protected float			overlayOpacity;
	protected int			currentFrame;
	protected static String TransitionType;
	public static Animator createAnimator(BufferedImage from, BufferedImage to, int transition, String transitionType) {
		setTransitionType(transition, transitionType);
		System.out.println("Max frame = " + MAX_FRAMES);
		Animator animator = null;
		switch (transition) {
		case -1:
			animator = null;// no effect
			break;
		case 0:
			animator = new FadeIn(from, to);
			break;
		case 1:
			animator = new CircleOut(from, to);
			break;
		case 2:
			animator = new Dissolve(from, to);
			break;
		case 3:
			System.out.println("Gravity started");
			animator = new Gravity(from, to);
			break;
		case 4:
			animator = new RandomBarTransition(from, to);
			break;
		case 5:
			animator = new SlideFromTop(from, to);
			break;
		case 6:
			animator = new SlideFromBottom(from, to);
			break;
		case 7:
			animator = new SlideFromRight(from, to);
			break;
		case 8:
			animator = new SlideFromLeft(from, to);
			break;
		case 9:
			animator = new WipeDown(from, to);
			break;
		case 10:
			animator = new WipeUp(from, to);
			break;
		case 11:
			animator = new WipeLeft(from, to);
			break;
		case 12:
			animator = new WipeRight(from, to);
			break;
		case 13:
			animator = new RollUp(from, to);
			break;
		case 14:
			animator = new RollDown(from, to);
			break;
		case 15:
			animator = new RollLeft(from, to);
			break;
		case 16:
			animator = new RollRight(from, to);
			break;
		case 17:
			animator = new CheckerBoard(from, to);
			break;
		case 19:
			animator = new DoorClose(from, to);
			break;
		case 18:
			animator = new DoorOpen(from, to);
			break;
		case 20:
			animator = new FadeInFast(from, to);
			break;
		case 21:
			animator = new CircleIn(from, to);
			break;
		default:
			animator = createAnimator(from, to, (int) (Math.random() * transition),transitionType);
		}
		return animator;
	}

	public static void setTransitionType(int transition, String transitionType)
	{
		switch (transition) {
		case -1:
			MAX_FRAMES = 0;// no effect
			break;
		case 0:					// FadeIn
		case 3:					// Gravity
			MAX_FRAMES	= 800;	
			break;
		case 1:					// CircleOut
		case 2:					// Dissolve
		case 4:					// RandomBarTransition
		case 5:					// SlideFromTop
		case 6:					// SlideFromBottom
		case 7:					// SlideFromRight
		case 8:					// SlideFromLeft
		case 9:					// WipeDown
		case 10:					// WipeUp
		case 11:					// WipeLeft
		case 12:					// WipeRight
		case 13:					// RollUp
		case 14:					// RollDown
		case 15:					// RollLeft	
		case 16:					// RollRight
			try
			{
				if(transitionType.equals("Normal")){ MAX_FRAMES = 300; }
				else if(transitionType.equals("Fast")){ MAX_FRAMES = 200; }
				else if(transitionType.equals("Slow")){ MAX_FRAMES = 700; }
				else	{ MAX_FRAMES = 400; }
			}catch(Exception e)
			{
				MAX_FRAMES = 400;
			}
			break;
		case 17:					//CheckerBoard
			break;
		case 19:					//DoorClose
		case 18:					// DoorOpen
			try
			{
				if(transitionType.equals("Normal")){ MAX_FRAMES = 300; }
				else if(transitionType.equals("Fast")){ MAX_FRAMES = 150; }
				else if(transitionType.equals("Slow")){ MAX_FRAMES = 600; }
				else	{ MAX_FRAMES = 300; }
			}catch(Exception e)
			{
				MAX_FRAMES = 300;
			}
			break;
		case 20:					// FadeInFast
			MAX_FRAMES	= 200;
			break;
		case 21:	
			MAX_FRAMES	= 800;
			break;
		}
	}

	public Animator(BufferedImage from, BufferedImage to) {
		this.original = from;
		this.overlay = to;
		this.currentFrame = 0;
		this.originalPosition = new Point(0, 0);
		this.overlayPosition = new Point(0, 0);
		this.overlayOpacity = 1;
		this.originalOpacity = 1;

	}

	public boolean isAnimatable() {
		return this.currentFrame < MAX_FRAMES && this.original.getWidth() == this.overlay.getWidth()
				&& this.original.getHeight() == this.overlay.getHeight();
	}

	public int getFrameNumber() {
		return this.currentFrame;
	}

	// plays one frame of a simple animation
	public abstract void animate();
}
